package org.szgb.console.sys.user.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.sys.role.entity.SysRole;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class SysUserRole extends BeanEntity<SysUserRole> {
    @Serial
    private static final long serialVersionUID = 1L;
    private SysUser user;
    private SysRole role;


    public SysUserRole() {
    }

    public SysUserRole(String id) {
        this.setId(id);
    }

    public SysUserRole(SysUserRole.SysUserRoleFindDto sysUserRoleFindDto) {
        this.setId(sysUserRoleFindDto.getId());
        this.setUser(new SysUser(sysUserRoleFindDto.getUserId()));
        this.setRole(new SysRole(sysUserRoleFindDto.getRoleId()));
        this.setPageDto(sysUserRoleFindDto.getPageDto());
    }

    public SysUserRole(SysUserRole.SysUserRoleSaveDto sysUserRoleSaveDto) {
        this.setId(sysUserRoleSaveDto.id);
        this.setUser(new SysUser(sysUserRoleSaveDto.userId));
        this.setRole(new SysRole(sysUserRoleSaveDto.roleId));
    }

    public SysUserRole.SysUserRoleView toSysUserRoleView() {
        return new SysUserRole.SysUserRoleView(this);
    }

    @Data
    @Schema(title = "用户角色信息", description = "用户角色信息")
    public static class SysUserRoleView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "用户id")
        private String userId;
        @Schema(description = "用户名称")
        private String userName;
        @Schema(description = "角色id")
        private String roleId;
        @Schema(description = "角色名称")
        private String roleName;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public SysUserRoleView(SysUserRole sysUserRole) {
            this.id = sysUserRole.getId();
            this.userId = sysUserRole.getUser().getId();
            this.userName = sysUserRole.getUser().getName();
            this.roleId = sysUserRole.getRole().getId();
            this.roleName = sysUserRole.getRole().getName();
            this.createdAt = sysUserRole.getCreatedAt();
            this.updatedAt = sysUserRole.getUpdatedAt();
            if (sysUserRole.getCreatedBy() != null) {
                this.createdById = sysUserRole.getCreatedBy().getId();
                this.createdByName = sysUserRole.getCreatedBy().getName();
            }
            if (sysUserRole.getUpdatedBy() != null) {
                this.updatedById = sysUserRole.getUpdatedBy().getId();
                this.updatedByName = sysUserRole.getUpdatedBy().getName();
            }
        }

        public static List<SysUserRole.SysUserRoleView> getViewListFromList(List<SysUserRole> sysUserRoleList) {
            return sysUserRoleList.stream().map(SysUserRole.SysUserRoleView::new).toList();
        }
    }

    @Data
    @Schema(title = "用户角色查询表单", description = "用户角色查询参数")
    public static class SysUserRoleFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "用户id")
        private String userId;
        @Schema(description = "角色id")
        private String roleId;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public SysUserRole toSysUserRole() {
            return new SysUserRole(this);
        }
    }

    @Data
    @Schema(title = "用户角色保存表单", description = "用户角色保存参数")
    public static class SysUserRoleSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "用户id")
        private String userId;
        @Schema(description = "角色id")
        private String roleId;

        public SysUserRole toSysUserRole() {
            return new SysUserRole(this);
        }
    }
}
